package com.link.base.base.language.controller;

import com.alibaba.fastjson.JSONObject;
import com.link.base.base.language.model.Language;
import com.link.base.base.language.service.LanguageService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.ReturnCodeConstants;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 语言包
 *
 * @author 阳晓琳
 * @date 2020-08-27 11:01:53
 */
@Controller
@RequestMapping("/link/language")
public class LanguageController extends BasicController<Language> {

    private static final Logger logger = LogManager.getLogger(LanguageController.class);

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private LanguageService languageService;

    @Override
    public BasicService<Language> getBasicService() throws Exception {
        return languageService;
    }

    /**
     * 获取语言包
     *
     * @author yxl
     * @date 2020/09/01 10:00:00
     */
    @RequestMapping(value = "/langPackage")
    @ResponseBody
    public Map<String, Object> langPackage(@JsonParam Language language, @JsonParam QueryParams qps, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            JSONObject entity = languageService.langPackage(language);
            result.put("success", true);
            result.put("locale", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
        }
        return result;
    }

    /**
     * 获取语言包
     *
     * @author yxl
     * @date 2020/09/01 10:00:00
     */
    @RequestMapping(value = "/refreshCache")
    @ResponseBody
    public Map<String, Object> refreshCache(@JsonParam Language language, @JsonParam QueryParams qps, HttpSession session,
                                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            languageService.refreshCache(language);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 获取语言包
     *
     * @author yxl
     * @date 2020/09/01 10:00:00
     */
    @RequestMapping(value = "/langList")
    @ResponseBody
    public Map<String, Object> langList(@JsonParam Language language, @JsonParam QueryParams qps, HttpSession session,
                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            List<Language> entity = languageService.langList(language);
            result.put("success", true);
            result.put("locale", entity);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
        }
        return result;
    }

    @RequestMapping(value = "/updateOtherLanguage")
    @ResponseBody
    public Map<String, Object> updateOtherLanguage(@RequestBody Language language) {
        Map<String, Object> result = new HashMap<>(2);
        try {
            languageService.updateOtherLanguage(language);
            result.put("success", true);
        } catch (Exception e) {
            logger.error("更新至其它语言包异常：{}", e.getMessage());
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/deleteByIdCascade")
    @ResponseBody
    public Map<String, Object> deleteByIdCascade(@JsonParam Language entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        logger.info("删除一条列表信息...");
        Map<String, Object> result = new HashMap<>(4);
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            if (!checkExistDataCache("base", session, entity)) {
                throw new BasicServiceException("非法删除数据！");
            }

            languageService.deleteByIdCascade(entity);
            result.put("success", true);
            result.put("code", ReturnCodeConstants.SUCCESS);
        } catch (BasicServiceException e) {
            logger.error("BasicController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
            result.put("code", e.getCode());
        } catch (Exception e) {
            logger.error("BasicController捕获异常: ", e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", ReturnCodeConstants.EXCEPTION);
        }
        return result;
    }
}
